﻿using AutoMapper;
using Common.Communal;
using ERP.Domain.System;
using MediatR;
using System.Api.Write.Applocations.Commands.Organizes;
using System.Services.Organizes;

namespace System.Api.Write.Applocations.CommandHandlers.Organizes
{
    /// <summary>
    /// 创建组织命令处理程序
    /// </summary>
    public class OrganizeCreateCommandHandler : IRequestHandler<OrganizeCreateCommand, ApiResult<int>>
    {
        private readonly IOrganizeService service;
        private readonly IMapper mapper;

        public OrganizeCreateCommandHandler(IOrganizeService service, IMapper mapper)
        {
            this.service = service;
            this.mapper = mapper;
        }

        /// <summary>
        /// 处理创建组织命令
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>处理结果</returns>
        public async Task<ApiResult<int>> Handle(OrganizeCreateCommand request, CancellationToken cancellationToken)
        {
            var res = mapper.Map<Organize>(request);
            return await service.Add(res);
        }
    }
}
